// ==================================================================================
// Shared Genomics Project MPI Codebase
// Version 1.0 30/04/2010
//
// (c) 2010 University of Manchester all rights reserved
//
// This file is distributed under the GNU General Public License, Version 2.  
// Please see the file COPYING.txt for more details
// ==================================================================================

/*!
\file
\brief Implementation of SNP-Major Allelic Data Storage
*/

#include <stdlib.h>
#include <stdio.h>

#include "csnp.h"

int csnp_init(struct csnp* c) {
	if (c != NULL) {
		c->nOne = 0;
		c->nTwo = 0;
		c->one = NULL;
		c->two = NULL;
		c->currentOneIndex = 0;
		c->currentTwoIndex = 0;

		return 1;
	}

	return 1;
}

struct csnp* csnp_create() {
	struct csnp* c = NULL;

	c = (struct csnp*) malloc(sizeof(struct csnp));
	csnp_init(c);

	return c;
}

int csnp_free_attributes(struct csnp *c) {
	if (c != NULL) {
		if (c->nOne > 0) {
			free(c->one);
			c->one = NULL;
			c->nOne = 0;
			c->currentOneIndex = 0;
		}
		if (c->nTwo > 0) {
			free(c->two);
			c->two = NULL;
			c->nTwo = 0;
			c->currentTwoIndex = 0;
		}
		
		return 1;
	}

	return 0;
}

int csnp_alloc_one(struct csnp *c, int size) {
	int i;	

	if (c == NULL || size == 0) return 0;
	// Already allocated so multiple invocations would cause a memory leak.
	else if (c->nOne > 0) return 0;

	c->one = (BOOL*) malloc(sizeof(BOOL) * size);
	if (c->one == NULL) return 0;
	else {
		for (i = 0; i < size; i++) {
			c->one[i] = FALSE;
		}
		c->nOne = size;
	}

	return 1;
}

int csnp_alloc_two(struct csnp *c, int size) {
	int i;	

	if (c == NULL || size == 0) return 0;
	// Already allocated so multiple invocations would cause a memory leak.
	else if (c->nTwo > 0) return 0;

	c->two = (BOOL*) malloc(sizeof(BOOL) * size);
	if (c->two == NULL) return 0;
	else {
		for (i = 0; i < size; i++) {
			c->two[i] = FALSE;
		}
		c->nTwo = size;
	}

	return 1;
}
